﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YJKArch.AreaStatistical.DataManager;
using YJKArch.Utils;

namespace YJKArch.AreaStatistical.SetRoomInfo
{
	public partial class SetRoomInfoForm : System.Windows.Forms.Form
	{
		public SetRoomInfoForm(RoomInfoCreator roomInfoCreator)
		{
			this.m_RoomInfoCreator = roomInfoCreator;
			this.InitializeComponent();
			this.tbNewName.ImeMode = ImeMode.OnHalf;
		}

		public RoomType TheRoomType
		{
			get
			{
				if (this.tabControl.SelectedIndex == 0)
				{
					return RoomType.RT_Hall;
				}
				if (this.tabControl.SelectedIndex == 1)
				{
					return RoomType.RT_Room;
				}
				if (this.tabControl.SelectedIndex == 2)
				{
					return RoomType.RT_Cell;
				}
				return RoomType.RT_Other;
			}
		}

		public RoomSettingInfo CurrentRoomSettingInfo
		{
			get
			{
				DataGridView currentDataGridView = this.GetCurrentDataGridView();
				DataGridViewRow dataGridViewRow = currentDataGridView.SelectedRows[0];
				string name = dataGridViewRow.Cells[0].Value.ToString();
				double coefficient = Convert.ToDouble(dataGridViewRow.Cells[1].Value.ToString());
				bool isBalcony = false;
				if (currentDataGridView.ColumnCount == 3)
				{
					isBalcony = Convert.ToBoolean(dataGridViewRow.Cells[2].Value.ToString());
				}
				return new RoomSettingInfo(name, coefficient, isBalcony);
			}
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SetRoomNameForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				SetRoomInfoForm.SendMessage(RoomInfoCreator._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SetRoomNameForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			SetRoomInfoForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			SetRoomInfoForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void btnAddName_Click(object sender, EventArgs e)
		{
			if (this.tbNewName.Text.Trim().CompareTo("") == 0)
			{
				return;
			}
			IEnumerator enumerator = ((IEnumerable)this.dbgHall.Rows).GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (((DataGridViewRow)enumerator.Current).Cells[0].Value.ToString().CompareTo(this.tbNewName.Text) == 0)
					{
						MessageBox.Show("新名称在'厅'列表中已经存在，无法重复添加", "盈建科提示");
						return;
					}
				}
			}
			enumerator = ((IEnumerable)this.dbgRoom.Rows).GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (((DataGridViewRow)enumerator.Current).Cells[0].Value.ToString().CompareTo(this.tbNewName.Text) == 0)
					{
						MessageBox.Show("新名称在'室'列表中已经存在，无法重复添加", "盈建科提示");
						return;
					}
				}
			}
			enumerator = ((IEnumerable)this.dbgCell.Rows).GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (((DataGridViewRow)enumerator.Current).Cells[0].Value.ToString().CompareTo(this.tbNewName.Text) == 0)
					{
						MessageBox.Show("新名称在'卫'列表中已经存在，无法重复添加", "盈建科提示");
						return;
					}
				}
			}
			enumerator = ((IEnumerable)this.dbgOther.Rows).GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (((DataGridViewRow)enumerator.Current).Cells[0].Value.ToString().CompareTo(this.tbNewName.Text) == 0)
					{
						MessageBox.Show("新名称在'其它'列表中已经存在，无法重复添加", "盈建科提示");
						return;
					}
				}
			}
			DataGridView currentDataGridView = this.GetCurrentDataGridView();
			double coefficient = 1.0;
			this.InsertDBGRow(currentDataGridView, this.tbNewName.Text, coefficient, false);
			this.tbNewName.Text = "";
		}

		private void InsertDBGRow(DataGridView dataGridView, string name, double coefficient, bool isBalcony)
		{
			DataGridViewRow dataGridViewRow = new DataGridViewRow();
			DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
			dataGridViewTextBoxCell.Value = name;
			dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
			DataGridViewTextBoxCell dataGridViewTextBoxCell2 = new DataGridViewTextBoxCell();
			dataGridViewTextBoxCell2.Value = coefficient;
			dataGridViewRow.Cells.Add(dataGridViewTextBoxCell2);
			if (dataGridView.ColumnCount == 3)
			{
				DataGridViewCheckBoxCell dataGridViewCheckBoxCell = new DataGridViewCheckBoxCell();
				dataGridViewCheckBoxCell.Value = isBalcony;
				dataGridViewRow.Cells.Add(dataGridViewCheckBoxCell);
			}
			dataGridView.Rows.Insert(0, dataGridViewRow);
			dataGridView.Rows[0].Selected = true;
			dataGridView.CurrentCell = dataGridView.Rows[0].Cells[0];
		}

		private void AddDBGRow(DataGridView dataGridView, string name, double coefficient, bool isBalcony)
		{
			DataGridViewRow dataGridViewRow = new DataGridViewRow();
			DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
			dataGridViewTextBoxCell.Value = name;
			dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
			DataGridViewTextBoxCell dataGridViewTextBoxCell2 = new DataGridViewTextBoxCell();
			dataGridViewTextBoxCell2.Value = coefficient;
			dataGridViewRow.Cells.Add(dataGridViewTextBoxCell2);
			if (dataGridView.ColumnCount == 3)
			{
				DataGridViewCheckBoxCell dataGridViewCheckBoxCell = new DataGridViewCheckBoxCell();
				dataGridViewCheckBoxCell.Value = isBalcony;
				dataGridViewRow.Cells.Add(dataGridViewCheckBoxCell);
			}
			dataGridView.Rows.Add(dataGridViewRow);
		}

		private void SetRoomInfoForm_Load(object sender, EventArgs e)
		{
			this.ReadRoomTypeSettingData();
			this.ReadData();
		}

		private void SetRoomInfoForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveRoomTypeSettingData();
			this.SaveData();
		}

		private void ReadRoomTypeSettingData()
		{
			List<RoomSettingInfo> list = new List<RoomSettingInfo>();
			List<RoomSettingInfo> list2 = new List<RoomSettingInfo>();
			List<RoomSettingInfo> list3 = new List<RoomSettingInfo>();
			List<RoomSettingInfo> list4 = new List<RoomSettingInfo>();
			RoomTypeSettingManager.ReadRoomTypeInfo(ref list, ref list2, ref list3, ref list4);
			foreach (RoomSettingInfo roomSettingInfo in list)
			{
				this.AddDBGRow(this.dbgHall, roomSettingInfo.m_Name, roomSettingInfo.m_Coefficient, false);
			}
			foreach (RoomSettingInfo roomSettingInfo2 in list2)
			{
				this.AddDBGRow(this.dbgRoom, roomSettingInfo2.m_Name, roomSettingInfo2.m_Coefficient, false);
			}
			foreach (RoomSettingInfo roomSettingInfo3 in list3)
			{
				this.AddDBGRow(this.dbgCell, roomSettingInfo3.m_Name, roomSettingInfo3.m_Coefficient, false);
			}
			foreach (RoomSettingInfo roomSettingInfo4 in list4)
			{
				this.AddDBGRow(this.dbgOther, roomSettingInfo4.m_Name, roomSettingInfo4.m_Coefficient, roomSettingInfo4.m_IsBalcony);
			}
		}

		private void SaveRoomTypeSettingData()
		{
			List<RoomSettingInfo> list = new List<RoomSettingInfo>();
			foreach (object obj in ((IEnumerable)this.dbgHall.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				string name = dataGridViewRow.Cells[0].Value.ToString();
				double coefficient = Convert.ToDouble(dataGridViewRow.Cells[1].Value.ToString());
				bool isBalcony = false;
				list.Add(new RoomSettingInfo(name, coefficient, isBalcony));
			}
			List<RoomSettingInfo> list2 = new List<RoomSettingInfo>();
			foreach (object obj2 in ((IEnumerable)this.dbgRoom.Rows))
			{
				DataGridViewRow dataGridViewRow2 = (DataGridViewRow)obj2;
				string name2 = dataGridViewRow2.Cells[0].Value.ToString();
				double coefficient2 = Convert.ToDouble(dataGridViewRow2.Cells[1].Value.ToString());
				bool isBalcony2 = false;
				list2.Add(new RoomSettingInfo(name2, coefficient2, isBalcony2));
			}
			List<RoomSettingInfo> list3 = new List<RoomSettingInfo>();
			foreach (object obj3 in ((IEnumerable)this.dbgCell.Rows))
			{
				DataGridViewRow dataGridViewRow3 = (DataGridViewRow)obj3;
				string name3 = dataGridViewRow3.Cells[0].Value.ToString();
				double coefficient3 = Convert.ToDouble(dataGridViewRow3.Cells[1].Value.ToString());
				bool isBalcony3 = false;
				list3.Add(new RoomSettingInfo(name3, coefficient3, isBalcony3));
			}
			List<RoomSettingInfo> list4 = new List<RoomSettingInfo>();
			foreach (object obj4 in ((IEnumerable)this.dbgOther.Rows))
			{
				DataGridViewRow dataGridViewRow4 = (DataGridViewRow)obj4;
				string name4 = dataGridViewRow4.Cells[0].Value.ToString();
				double coefficient4 = Convert.ToDouble(dataGridViewRow4.Cells[1].Value.ToString());
				bool isBalcony4 = Convert.ToBoolean(dataGridViewRow4.Cells[2].Value.ToString());
				list4.Add(new RoomSettingInfo(name4, coefficient4, isBalcony4));
			}
			RoomTypeSettingManager.WriteRoomTypeInfo(list, list2, list3, list4);
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "SetRoomInfoFormData.dat";
			if (!File.Exists(path))
			{
				this.dbgHall.Rows[0].Selected = true;
				this.dbgRoom.Rows[0].Selected = true;
				this.dbgCell.Rows[0].Selected = true;
				this.dbgOther.Rows[0].Selected = true;
				base.StartPosition = FormStartPosition.CenterScreen;
				this.tabControl.SelectedIndex = 0;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			int selectedIndex = Convert.ToInt32(streamReader.ReadLine());
			this.tabControl.SelectedIndex = selectedIndex;
			int num = Convert.ToInt32(streamReader.ReadLine());
			if (num > -1 && num < this.dbgHall.Rows.Count)
			{
				this.dbgHall.Rows[num].Selected = true;
				this.dbgHall.CurrentCell = this.dbgHall.Rows[num].Cells[0];
			}
			num = Convert.ToInt32(streamReader.ReadLine());
			if (num > -1 && num < this.dbgRoom.Rows.Count)
			{
				this.dbgRoom.Rows[num].Selected = true;
				this.dbgRoom.CurrentCell = this.dbgRoom.Rows[num].Cells[0];
			}
			num = Convert.ToInt32(streamReader.ReadLine());
			if (num > -1 && num < this.dbgCell.Rows.Count)
			{
				this.dbgCell.Rows[num].Selected = true;
				this.dbgCell.CurrentCell = this.dbgCell.Rows[num].Cells[0];
			}
			num = Convert.ToInt32(streamReader.ReadLine());
			if (num > -1 && num < this.dbgOther.Rows.Count)
			{
				this.dbgOther.Rows[num].Selected = true;
				this.dbgOther.CurrentCell = this.dbgOther.Rows[num].Cells[0];
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "SetRoomInfoFormData.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.tabControl.SelectedIndex);
			streamWriter.WriteLine(this.dbgHall.SelectedRows[0].Index);
			streamWriter.WriteLine(this.dbgRoom.SelectedRows[0].Index);
			streamWriter.WriteLine(this.dbgCell.SelectedRows[0].Index);
			streamWriter.WriteLine(this.dbgOther.SelectedRows[0].Index);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private DataGridView GetCurrentDataGridView()
		{
			DataGridView result = this.dbgOther;
			if (this.tabControl.SelectedIndex == 0)
			{
				result = this.dbgHall;
			}
			else if (this.tabControl.SelectedIndex == 1)
			{
				result = this.dbgRoom;
			}
			else if (this.tabControl.SelectedIndex == 2)
			{
				result = this.dbgCell;
			}
			return result;
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			DataGridView currentDataGridView = this.GetCurrentDataGridView();
			if (currentDataGridView.Rows.Count == 1)
			{
				return;
			}
			int index = currentDataGridView.SelectedRows[0].Index;
			currentDataGridView.Rows.RemoveAt(index);
			if (index == 0)
			{
				currentDataGridView.Rows[0].Selected = true;
				return;
			}
			currentDataGridView.Rows[index - 1].Selected = true;
		}

		private void btnAutoSet_Click(object sender, EventArgs e)
		{
			this.SaveRoomTypeSettingData();
			this.m_RoomInfoCreator.AutoSet();
		}

		private void dbgHall_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dbgHall.CurrentCell.ColumnIndex == 1)
			{
				System.Windows.Forms.TextBox textBox = e.Control as System.Windows.Forms.TextBox;
                if (textBox != null)
				{
					textBox.TextChanged += this.DGVTextBoxTextChanged;
				}
			}
		}

		private void dbgRoom_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dbgRoom.CurrentCell.ColumnIndex == 1)
			{
				System.Windows.Forms.TextBox textBox = e.Control as System.Windows.Forms.TextBox;
				if (textBox != null)
				{
					textBox.TextChanged += this.DGVTextBoxTextChanged;
				}
			}
		}

		private void dbgCell_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dbgCell.CurrentCell.ColumnIndex == 1)
			{
				System.Windows.Forms.TextBox textBox = e.Control as System.Windows.Forms.TextBox;
				if (textBox != null)
				{
					textBox.TextChanged += this.DGVTextBoxTextChanged;
				}
			}
		}

		private void dbgOther_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dbgOther.CurrentCell.ColumnIndex == 1)
			{
				System.Windows.Forms.TextBox textBox = e.Control as System.Windows.Forms.TextBox;
				if (textBox != null)
				{
					textBox.TextChanged += this.DGVTextBoxTextChanged;
				}
			}
		}

		private void DGVTextBoxTextChanged(object sender, EventArgs e)
		{
			string text = ((System.Windows.Forms.TextBox)sender).Text;
			double num = 0.0;
			if (!this.ValidatingDouble(text, ref num))
			{
				((System.Windows.Forms.TextBox)sender).Text = "1.0";
			}
			this.Refresh();
		}

		private bool ValidatingDouble(string strValue, ref double dValue)
		{
			double num = 0.0;
			try
			{
				num = Convert.ToDouble(strValue);
			}
			catch (Exception)
			{
				return false;
			}
			if (Geometry.Greaterthan_Or_Equal(num, 0.0) && Geometry.Lessthan_Or_Equal(num, 1.0))
			{
				dValue = num;
				return true;
			}
			MessageBox.Show("数值范围：0.0～1.0", "盈建科提示");
			return false;
		}

		private void dbgOther_CurrentCellDirtyStateChanged(object sender, EventArgs e)
		{
			this.dbgOther.CommitEdit(DataGridViewDataErrorContexts.Commit);
		}

		private void dbgHall_CurrentCellDirtyStateChanged(object sender, EventArgs e)
		{
			this.dbgHall.CommitEdit(DataGridViewDataErrorContexts.Commit);
		}

		private void dbgRoom_CurrentCellDirtyStateChanged(object sender, EventArgs e)
		{
			this.dbgRoom.CommitEdit(DataGridViewDataErrorContexts.Commit);
		}

		private void dbgCell_CurrentCellDirtyStateChanged(object sender, EventArgs e)
		{
			this.dbgCell.CommitEdit(DataGridViewDataErrorContexts.Commit);
		}

		private RoomInfoCreator m_RoomInfoCreator;
	}
}
